| Total Complexity | 7 |
| Total Lines | 43 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import Relation from "../Relation.js"; |
||
| 3 | |||
| 4 | export default class BelongsTo extends Relation { |
||
| 5 | |||
| 6 | constructor(model: ModelStaticInterface, foreignKey: string|null = null, name: string) { |
||
| 7 | super(model, (foreignKey ?? `${model.snakeCaseClassName}_id`), name); |
||
| 8 | } |
||
| 9 | |||
| 10 | get originalValue() { |
||
| 11 | return this.model.find(this.$parent[`original_${this.foreignKey}`]); |
||
| 12 | } |
||
| 13 | |||
| 14 | get value() { |
||
| 15 | return this.model.find(this.$parent[this.foreignKey]); |
||
| 16 | } |
||
| 17 | |||
| 18 | // eslint-disable-next-line @typescript-eslint/no-empty-function |
||
| 19 | set value(value) { |
||
| 20 | |||
| 21 | } |
||
| 22 | |||
| 23 | setName(): BelongsTo { |
||
| 24 | const className = this.model.snakeCaseClassName; |
||
| 25 | this.$name = this.$name ?? `${className}`; |
||
| 26 | return this; |
||
| 27 | } |
||
| 28 | |||
| 29 | protected setParentProperties() { |
||
| 30 | super.setParentProperties(); |
||
| 31 | |||
| 32 | let name = ''; |
||
| 33 | for (const namePart of this.$name.split('_')) { |
||
| 34 | name += namePart[0].toUpperCase() + namePart.slice(1); |
||
| 35 | } |
||
| 36 | |||
| 37 | Object.defineProperty(this.$parent, |
||
| 38 | `has${name}`, { |
||
| 39 | get: () => { |
||
| 40 | return this.value !== null; |
||
| 41 | }, |
||
| 42 | } |
||
| 43 | ) |
||
| 44 | |||
| 45 | return this; |
||
| 46 | } |
||
| 47 | } |